package number_696;

public class Solution {
    //O(n^2);
    public int countBinarySubstrings(String s) {
        if (s.length() < 1) {
            return 0;
        }
        if (s.length() == 2) {
            return s.charAt(0) == s.charAt(1) ? 0 : 1;
        }
        int cnt = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            char left = s.charAt(i);
            char right = s.charAt(i + 1);
            if (left != right) {
                int leftIndex = i;
                int rightIndex = i + 1;
                while (leftIndex >= 0 && rightIndex < s.length() && left == s.charAt(leftIndex) &&
                right == s.charAt(rightIndex)) {
                    cnt++;
                    leftIndex--;
                    rightIndex++;
                }
            }

        }
        return cnt;
    }

    //O(n);
    public int countBinarySubstrings1(String s) {
        int ptr = 0, n = s.length(), last = 0, ans = 0;
        while (ptr < n) {
            char c = s.charAt(ptr);
            int count = 0;
            while (ptr < n && c == s.charAt(ptr)) {
                ptr++;
                count++;
            }
            ans += Math.min(last, count);
            last = count;

        }
        return ans;
    }

    public static void main(String[] args) {
        new Solution().countBinarySubstrings("00110011");
    }
}
